import { isNotEmpty } from '@/utils/validate'
import useStore from '@/store'

/**
 * 权限指令
 */
export default {
  mounted(el, binding) {
    checkRole(el, binding.value)
  },
  updated(el, binding) {
    checkRole(el, binding.value)
  },
}

function checkRole(el, value) {
  if (isNotEmpty(value)) {
    const { user } = useStore()
    if (!user.roles.includes(value)) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    new Error(`[v-auth]: The "value" must not be blank`)
  }
}
